<?php
declare(strict_types=1);

namespace mi7soft\core;
defined('MI7') or exit('No direct script access allowed');

function myException($exception)
{
    echo "<b>我设置的顶层异常处理器Exception:</b> " , $exception->getMessage();
}
set_exception_handler('myException');
/**
 * 全局配制
 */

use ArithmeticError;
use DivisionByZeroError;
use Exception;
use mi7soft\core\Config;
use mi7soft\core\Logger;
use mi7soft\core\Context;
use mi7soft\core\MainException;
use Throwable;


function getMicroTime()
{
    $num = round(microtime(true) * 1000, 0);
    return $num;
}

class A
{
    var $a;

    function printA()
    {

    }
}


class B
{
    var $a = 'a';
    private array $properties = [];

    public function __get($_name)
    {
        if (!isset($this->$_name)) {

            if (!isset($this->properties[$_name])) {
                $this->lazy_load($_name);
            }
            return $this->properties[$_name];

        } else {
            return $this->$_name;
        }
    }


    function printA()
    {

    }

    public function lazy_load($_name)
    {
        $this->properties[$_name] = $_name;
    }
}


trait Member
{
    public $aaa = 123;

    public $user_name;

    public function info()
    {


//        try {
//            $x = 4;
//            $y = 0;
//            $result = $x / $y;
//        } catch (Throwable   $e) {
//            throw new MainException($e->getMessage());
//        }

        $show_member = new ShowMember();
        $show_member->user_name = 'gztgztgztgztgztgztgztgztgztgztgztgztgztgztgztgztgztgzt';
        try {
            echo $show_member['user_name'];
        } catch (\Swow\Exception $e) {
            print_r($e);
        }
//        throw new MainException($this->aaa . "不存在");
//        echo 'this is trait' . $this->aaa;
//        config::get_int("abc");
    }

}

class ShowMember
{
    use Member;
}


class Main
{
    public Context $context;

    function __construct()
    {
        $this->context = new Context();
//        logger::init();
//        config::init();
//
    }

    /**
     *
     * @return \mi7soft\core\Context
     */
    public function &run(): Context
    {


        try {
            $name = 'gzt';
            (new ShowMember())->info();

        } catch (MainException $e) {
            echo $e->myerrorMessage() . "\n";
        }


//
//        $num = 0;
////        try {
//            $this->abc;
//            echo 1 / $num;
////        } catch (Exception $e) {
////            echo $e->getMessage();
////            echo 'here';
////        }
//
//        $a = 1;
//        $b = 2;
//        try {
//
////            //结果为真，设置异常信息
////            if ($a < $b) {
////                throw new Exception('a要大于b');
////            }
////            echo '正常';        //如果出现错误则不会执行  直接就会执行 catch 抛出异常
//        } catch (Throwable $e) {
//            // 返回异常信息
////             echo $e->getMessage() . ' zyf';
//        }
//
//        $x = "Exception";
//        y = "Handling";
//        echo $x . ' ' . y;
//

        return $this->context->end();
    }

    public function index()
    {
        global $configs;

        $a = getMicroTime();

        Logger::log("abc");

        $b = getMicroTime();
//        echo $b - $a;
//        echo "\n";

    }
}